<?php
global $_GPC, $_W;
$uniacid = $_W['uniacid'];
$id = $_GPC['id'];
$op = $_GPC['op'];
$res = $this->res;
$plus = $this->plus;
load()->model('mc');
load()->func('tpl');
$setting = $this->baseset($uniacid);
$jiangli = $setting['jiangli'] / 100;
$jiangli2 = $setting['jiangli2'] / 100;
$sdbili = intval($setting['sdbili']);
$settings = $this->module['config'];
if (empty($setting)) {
	message('请先进行参数设置！', referer(), 'error');
}
if ($setting['isshow'] != 1) {
	message('网站已关闭晒单功能！', $this->createMobileUrl('index'), 'error');
}
$openid = $_W['openid'];
$userinfo = $this->Get_checkoauth();
if (empty($_W['openid'])) {
	$openid = $userinfo['openid'];
}
$follow = $userinfo['follow'];
$user = $this->getuserinfo($openid, $userinfo['nickname'], $userinfo['avatar'], $follow);
$allcate = pdo_fetchall("SELECT * FROM " . tablename($this->tablecate) . " WHERE uniacid = '{$_W['uniacid']} ' AND status=1 order by displayorder desc");
$indexcate = pdo_fetchall("SELECT * FROM " . tablename($this->tablecate) . " WHERE uniacid = '{$_W['uniacid']} ' AND isindex=1 order by displayorder desc  LIMIT 7");
$_share['title'] = $setting['fxtit'];
$_share['imgUrl'] = tomedia($setting['fxlogo']);
$_share['desc'] = $setting['fxdes'];
$_share['link'] = $_W['siteroot'] . "app/index.php?i=" . $_W['uniacid'] . "&c=entry&do=index&m=bsht_tbk";
$_W['page']['sitename'] = $setting['sitetitle'];
if (empty($_W['page']['sitename'])) {
	$pagename = '晒单广场';
} else {
	$pagename = $_W['page']['sitename'];
}
if ($_W['ispost'] && $op == 'addajax') {
	$set = pdo_fetch('SELECT * FROM ' . tablename($this->tableshow) . ' WHERE uniacid = :uniacid AND openid = :openid AND orderid = :orderid', array(':uniacid' => $_W['uniacid'], ':openid' => $user['openid'], ':orderid' => $_GPC['order']));
	$orderid = pdo_fetch('SELECT * FROM ' . tablename($this->tableshow) . ' WHERE uniacid = :uniacid AND orderid = :orderid', array(':uniacid' => $_W['uniacid'], ':orderid' => $_GPC['order']));
	if ($orderid) {
		exit(json_encode(array('status' => '0', 'info' => "请不要重复晒单！", 'id' => $orderid['id'])));
	}
	if ($setting['isshowgz'] == 1) {
		if ($userinfo['follow'] != 1 || empty($userinfo['openid'])) {
			exit(json_encode(array('status' => '500', 'msg' => "没有关注")));
		}
	}
	for ($x = 0; $x < count($_GPC['img']); $x++) {
		$reimg = $this->doMobileMedia(array('media_id' => $_GPC['img'][$x], 'type' => 'image'));
		if (is_array($reimg)) {
			exit(json_encode(array('status' => '0', 'msg' => $reimg['message'] . "(" . $reimg['errno'] . ")")));
		} else {
			$img[] = $reimg;
		}
	}
	$data = array('uniacid' => $_W['uniacid'], 'openid' => $user['openid'], 'avatar' => $user['avatar'], 'nickname' => $user['nickname'], 'star' => $_GPC['grade'], 'orderfee' => $_GPC['price'], 'orderid' => $this->strsafe($_GPC['order']), 'img1' => $img[0], 'img2' => $img[1], 'img3' => $img[2], 'img4' => $img[3], 'img5' => $img[4], 'ctime' => time(), 'dec' => htmlspecialchars_decode($_GPC['evaluation'], ENT_QUOTES),);
	if ($setting['showisstatus'] == 1) {
		$data['status'] = 0;
	} else {
		$data['status'] = 1;
	}
	$jifen = $setting['showjifen'];
	$picjifen = $setting['showpicjifen'];
	if ($img[0]) {
		$jifenre = 0;
	}
	if ($img[1]) {
		$jifenre = $picjifen;
	}
	if ($img[2]) {
		$jifenre = $picjifen * 2;
	}
	if ($img[3]) {
		$jifenre = $picjifen * 3;
	}
	if ($img[4]) {
		$jifenre = $picjifen * 4;
	}
	if ($setting['showisstatus'] == 1) {
		$data['status'] = 0;
		$data['jifen'] = $jifenre + $jifen;
	} else {
		$thisorder = pdo_fetch("SELECT * FROM " . tablename($this->tableorder) . " WHERE uniacid = '{$_W['uniacid']}' AND orderid = '{$_GPC['order']}'");
		if ($thisorder['orderstatus'] == "订单结算") {
			$plusjifen = $thisorder['shouru_yongjin'] * $sdbili;
			$data['queren'] = 1;
			$data['status'] = 1;
			$data['jifen'] = $jifenre + $jifen + $plusjifen;
		} else {
			$data['jifen'] = $jifenre + $jifen;
			$data['status'] = 0;
		}
	}
	if (empty($set)) {
		pdo_insert($this->tableshow, $data);
		$insertid = pdo_insertid();
		if ($insertid) {
			if ($setting['showisstatus'] == 0 && $thisorder['orderstatus'] == "订单结算") {
				$poster = pdo_fetch("SELECT * FROM " . tablename('bsht_tbk_poster') . " WHERE uniacid = :uniacid order by id asc", array(':uniacid' => $_W['uniacid']));
				$showuser = pdo_fetch('SELECT * FROM ' . tablename($this->tableshow) . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $insertid));
				$postuser1 = pdo_fetch('SELECT * FROM ' . tablename($this->tableuser) . ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], ':openid' => $showuser['openid']));
				$postuser2 = pdo_fetch('SELECT * FROM ' . tablename($this->tableuser) . ' WHERE uniacid = :uniacid AND uid=:uid', array(':uniacid' => $_W['uniacid'], ':uid' => $postuser1['helpid']));
				$rejifen = $showuser['jifen'];
				$toopenid = $postuser1['openid'];
				$touid = mc_openid2uid($toopenid);
				$status = mc_credit_update($touid, 'credit1', $rejifen, array($touid, '审核晒单,奖励【' . $rejifen . '】积分'));
				$mcinfo = mc_fetch($touid);
				$mc_data = array('credit1' => $mcinfo['credit1'], 'credit2' => $mcinfo['credit2'],);
				pdo_update($this->tableuser, $mc_data, array('uid' => $touid, 'uniacid' => $_W['uniacid']));
				if ($poster['cscore'] > 0) {
					$touid1 = $postuser1['helpid'];
					if ($touid1 != 0) {
						$status1 = mc_credit_update($touid1, 'credit1', $rejifen * $jiangli, array($touid1, '被邀请人晒单订单,奖励【' . $rejifen * $jiangli . '】积分'));
						$mcinfo = mc_fetch($touid1);
						$mc_data = array('credit1' => $mcinfo['credit1'], 'credit2' => $mcinfo['credit2'],);
						pdo_update($this->tableuser, $mc_data, array('uid' => $touid1, 'uniacid' => $_W['uniacid']));
					}
				}
				if ($poster['pscore'] > 0) {
					$touid2 = $postuser2['helpid'];
					if ($touid2 != 0) {
						$status2 = mc_credit_update($touid2, 'credit1', $rejifen * $jiangli2, array($touid2, '被邀请人晒单审核,奖励【' . $rejifen * $jiangli2 . '】积分'));
						$mcinfo = mc_fetch($touid2);
						$mc_data = array('credit1' => $mcinfo['credit1'], 'credit2' => $mcinfo['credit2'],);
						pdo_update($this->tableuser, $mc_data, array('uid' => $touid2, 'uniacid' => $_W['uniacid']));
					}
				}
				$this->temp_ms_sh($showuser['openid'], "晒单审核，奖励" . $rejifen . "积分", $settings['sd_mshead'], $settings['sd_msfoot'], $settings['sd_msdo'], $settings['sd_msmain']);
			}
			$this->temp_ms_gl('有奖晒单', '有用户发布晒单，请尽快审核！', 'show_dan');
			exit(json_encode(array('status' => '1', 'info' => '发布成功，请等待审核！')));
		} else {
			exit(json_encode(array('status' => '0', 'info' => '发生错误，请重试！')));
		}
	} else {
		exit(json_encode(array('status' => '0', 'info' => '请不要重复晒单！', 'id' => $set['id'])));
	}
}
if ($_W['ispost'] && $op == 'indexajax') {
	$nowpage = $_GPC['limit'];
	$pindex = max(1, intval($nowpage));
	$psize = 10;
	$list = pdo_fetchall('SELECT * FROM ' . tablename($this->tableshow) . ' WHERE uniacid = :uniacid AND status=1 ORDER BY `ctime` DESC,`id` DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $uniacid));
	if (!empty($keyword) && empty($list)) {
		exit(json_encode(array('status' => 301, 'content' => $list)));
	}
	if (!empty($list)) {
		foreach ($list as $key => $value) {
			$list[$key]['avatar'] = tomedia($list[$key]['avatar']);
			$list[$key]['picview'] = "{pureview: true}";
			if ($list[$key]['img1']) {
				$list[$key]['pics'] = '
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img1']) . '" style="width:90px;height:90px;" />
		';
			}
			if ($list[$key]['img2']) {
				$list[$key]['pics'] = '
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img1']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img2']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		';
			}
			if ($list[$key]['img3']) {
				$list[$key]['pics'] = '
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img1']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img2']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img3']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		';
			}
			if ($list[$key]['img4']) {
				$list[$key]['pics'] = '
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img1']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img2']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img3']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img4']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		';
			}
			if ($list[$key]['img5']) {
				$list[$key]['pics'] = '
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img1']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img2']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img3']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img4']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		<img class="loazd lazy-bk" src="../addons/bsht_tbk/res/images/load.gif" data-bk="' . tomedia($list[$key]['img5']) . '" style="width:90px;height:90px;" data-am-pureviewed="1"/>
		';
			}
			if ($list[$key]['star'] == 1) {
				$list[$key]['star'] = '<i class="am-icon-star"></i>';
			}
			if ($list[$key]['star'] == 2) {
				$list[$key]['star'] = '<i class="am-icon-star"></i><i class="am-icon-star"></i>';
			}
			if ($list[$key]['star'] == 3) {
				$list[$key]['star'] = '<i class="am-icon-star"></i><i class="am-icon-star"></i><i class="am-icon-star"></i>';
			}
			if ($list[$key]['star'] == 4) {
				$list[$key]['star'] = '<i class="am-icon-star"></i><i class="am-icon-star"></i><i class="am-icon-star"></i><i class="am-icon-star"></i>';
			}
			if ($list[$key]['star'] == 5) {
				$list[$key]['star'] = '<i class="am-icon-star"></i><i class="am-icon-star"></i><i class="am-icon-star"></i><i class="am-icon-star"></i><i class="am-icon-star"></i>';
			}
		}
		$sta = 200;
	} else {
		$sta = -103;
	}
	exit(json_encode(array('status' => $sta, 'content' => $list)));
}
if (empty($op) || $op == 'index') {
	$nowpage = $_GPC['page'];
	if (empty($nowpage)) {
		$nowpage = 1;
	}
	$pindex = max(1, intval($nowpage));
	if ($setting['pageno']) {
		$psize = $setting['pageno'];
	} else {
		$psize = 10;
	}
	$list = pdo_fetchall('SELECT * FROM ' . tablename($this->tableshow) . ' WHERE uniacid = :uniacid AND status=1 ORDER BY `ctime` DESC,`id` DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $uniacid));
	include $this->template('show_dan_index');
} else if ($op == 'add') {
	include $this->template('show_dan_add');
} else if ($op == 'my') {
	include $this->template('show_dan_my');
} else if ($op == 'rank') {
	include $this->template('show_dan_rank');
}